//
//  LMlistTableViewController.m
//  20.私人通讯录
//
//  Created by 张青花 on 15/8/27.
//  Copyright (c) 2015年 李明. All rights reserved.
//

#import "LMlistTableViewController.h"
#import "LMPhone.h"
#import "LMAddViewController.h"
#import "LMEditViewController.h"
#import "LMListTableViewCell.h"
#define CONTACT_PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"list.arc"]

@interface LMlistTableViewController ()<UIActionSheetDelegate,LMAddViewControllerDeleage,LMEditViewControllerDeleage>

@property (nonatomic,strong)NSMutableArray *phoneArray;


@property (nonatomic,strong)UIActionSheet *sheet;

@end

@implementation LMlistTableViewController

-(NSMutableArray *)phoneArray
{
    if (_phoneArray==nil)
    {
        
        _phoneArray = [NSKeyedUnarchiver unarchiveObjectWithFile:CONTACT_PATH];
        if (_phoneArray == nil )
        {
            _phoneArray = [NSMutableArray array];
        }

    }
    return _phoneArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (IBAction)loginOut:(UIBarButtonItem *)sender
{
     self.sheet = [[UIActionSheet alloc]initWithTitle:@"你确定要注销吗？" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil];
    
    [self.sheet showInView:self.view];

    
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    UIViewController *vc = segue.destinationViewController;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    
    
    if([vc isKindOfClass:[LMAddViewController class]])
    {
        LMAddViewController *addcontroller = (LMAddViewController *)vc;
        
        addcontroller.deleage = self;
    }
    else
    {
        
     
        LMEditViewController *editcontroller = (LMEditViewController *)vc;
        
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        editcontroller.phone = self.phoneArray[path.row];
        
        editcontroller.deleage =self;
        
        
    }
        
    
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (0 == buttonIndex) {
     
       [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.phoneArray removeObjectAtIndex:indexPath.row];
    
    NSIndexPath *indexdelet = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    
    [self.tableView deleteRowsAtIndexPaths:@[indexdelet] withRowAnimation:UITableViewRowAnimationFade];
    [NSKeyedArchiver archiveRootObject:self.phoneArray toFile:CONTACT_PATH];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.phoneArray.count;
    
   
}

#pragma mark - Table view data deleage

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *tablecell = [tableView dequeueReusableCellWithIdentifier:@"listcell"];
//    
//    LMPhone *phone =self.phoneArray[indexPath.row];
//    
//    tablecell.textLabel.text =phone.username;
//    tablecell.detailTextLabel.text = phone.number;
    
    LMListTableViewCell *tablecell = [LMListTableViewCell listTableViewCell:tableView];
    LMPhone *phone =self.phoneArray[indexPath.row];
    
    tablecell.phone = phone;
    return tablecell;
}


#pragma mark - LMAddViewControllerDeleage
- (void) addViewController:(LMAddViewController *)addController phone:(LMPhone *)phone
{
    
    [self.phoneArray addObject:phone];
    
    [NSKeyedArchiver archiveRootObject:self.phoneArray toFile:CONTACT_PATH];
    
    [self.tableView reloadData];
    
}

#pragma mark - LMEditViewController
- (void) editViewController:(LMEditViewController *)addController phone:(LMPhone *)phone
{
    
    [NSKeyedArchiver archiveRootObject:self.phoneArray toFile:CONTACT_PATH];
    
    [self.tableView reloadData];
    
}
@end
